#include <Windows.h>
#include <MMSystem.h>

class DogSound 
{
	bool continueProcessing;
	bool pause;
public:

	DogSound();
	~DogSound();

	void Init();
	void UnInit();
	void Bark();

	HANDLE soundThread;

	DWORD WINAPI SoundThread();
	static DWORD WINAPI SoundThread(LPVOID pParam);
	void Pause();
	void Continue();
	void PlayTurnMusic();
};

void DogSound::Pause()
{
	pause = true;
}

void DogSound::Continue()
{
	pause = false;
}

void DogSound::PlayTurnMusic()
{
	PlaySound(TEXT("funny.wav"), NULL, SND_FILENAME | SND_ASYNC);
}

void DogSound::Bark()
{			
	if( !pause )
	{
		int randomNumber = rand() % 5;
		if(randomNumber == 0)
			PlaySound(TEXT("bark1.wav"), NULL, SND_FILENAME | SND_SYNC);
		else if(randomNumber == 1)
			PlaySound(TEXT("bark2.wav"), NULL, SND_FILENAME | SND_SYNC);
		else if(randomNumber == 2)
			PlaySound(TEXT("bark3.wav"), NULL, SND_FILENAME | SND_SYNC);
		else if(randomNumber == 3)
			PlaySound(TEXT("bark4.wav"), NULL, SND_FILENAME | SND_SYNC);
		else
			PlaySound(TEXT("bark5.wav"), NULL, SND_FILENAME | SND_SYNC);
					 
	}
}

DogSound::DogSound() { pause = false; }

DogSound::~DogSound() { }

DWORD WINAPI DogSound::SoundThread(LPVOID pParam)
{
	DogSound *pthis = (DogSound *) pParam;
	return pthis->SoundThread();
}

DWORD WINAPI DogSound::SoundThread()
{
	// Main thread loop
	continueProcessing = true;
	while ( continueProcessing )
	{	
		Bark();
		Sleep(100);
	}

	return 0;
}

void DogSound::Init()
{
	soundThread = CreateThread( NULL, 0, SoundThread, this, 0, NULL );
}

void DogSound::UnInit()
{
	continueProcessing = false;
	// Wait for thread to stop
	if ( NULL != soundThread )
	{
		WaitForSingleObject( soundThread, INFINITE );
		CloseHandle( soundThread );
	}
}